package com.iee.file;

import com.iee.spring.configdemo.Test1;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 *@ClassName CleanClass
 *@Description TODO
 *@Author longxiaonan@163.com
 *@Date 2020/9/21 0021 13:05
 */
public class CleanClass {

    public static void main(String[] args) throws IOException {

        List<String> stringList = Files.readAllLines(Paths.get("C:\\Users\\Administrator\\Desktop\\22\\3\\className.txt"));

        String filePath = "C:\\Users\\Administrator\\Desktop\\22\\3\\com";
        File dir = new File(filePath);
        System.out.println("------------------------------");
        System.out.println("获取到的文件夹路径是:" + dir);
        System.out.println("------------------------------");
        System.out.println("开始删除" + dir + "文件夹!");
        deleFile(dir, stringList);
        deleEemp(dir);
    }

    private static void deleEemp(File dir) {
        File[] subFiles = dir.listFiles();
        if(null != subFiles) {
            for (File subFile : subFiles) {
                System.out.println(subFile.getName());
//            if(subFile.isFile()){
                if(null != subFile) {
                    File[] files = subFile.listFiles();
                    if (subFile.isDirectory() && (null == files)) {
                        subFile.delete();
                    }
                }else {
                    subFile.delete();
                }
            }
        }
    }


    public static void deleFile(File dir, List<String> stringList) {
        File[] subFiles = dir.listFiles();
        if(null != subFiles) {
            for (File subFile : subFiles) {
                System.out.println(subFile.getName());
//            if(subFile.isFile()){
                if(null != subFile) {
                    if (subFile.isFile() && !stringList.contains(subFile.getName())) {
                        subFile.delete();
                    } else {
                        deleFile(subFile, stringList);
                    }
                }
            }
        }
//        dir.delete();
        System.out.println("------------------------------");
        System.out.println(dir + "文件夹被删除!");
    }

}
